new Vue({
	el: '#app',
	data: {
		id: '',
		uid: '',
		message: '',
		path: '',
		lPath: '',
		topic: [],
		questionList: [],
		page: '1',
		t: [],
		isShow: false
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			self.uid = $api.getStorage('uid');
			self.getMain();
		},
		getMain: function() {
			var self = this;
			console.log(self.uid);
			//self.getQuestionList();
			//self.getTopic();
			apiready = function() {
				//loading
				self.isS();
				//监听
				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.getQuestionList();
					}
				});
			};
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.getQuestionList();
				self.getTopic();
			}
		},
		//获得分类话题
		getTopic: function() {
			var self = this;
			console.log(self.uid);
			var data = {};
			$.post(base + config.sns_topic_category_index, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.topic = msg.data;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						var mySwiper = new Swiper('.swiper-container', {
							slidesPerView: 'auto', //'auto'
							slidesPerView: 3.7,
						})
					});
				} else {
					console.log(self.error_msg);
					api.toast({
						msg: self.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//获得问题列表
		getQuestionList: function() {
			var self = this;
			var data = {
				page: self.page
			};
			$.post(base + config.sns_question_index, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.data;
					for(var i = 0; i < self.t.length; i++) {
						self.questionList.push(self.t[i]);
					};
					self.pageCount = msg.page_count;
					self.lPath = msg.url;
					self.isShow = true;
					self.isS();
				} else {
					console.log(self.error_msg);
					api.toast({
						msg: self.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//跳转到该分类话题下的所有话题
		jump: function(id, name) {
			api.openWin({
				name: 'topic-list',
				url: 'topicList.html',
				vScrollBarEnabled: false,
				pageParam: {
					id: id,
					name: name
				}
			});
		},
		//跳转到该问题的详情
		jumpDetails: function(id) {
			api.openWin({
				name: 'question-details',
				url: 'questionDetails.html',
				vScrollBarEnabled: false,
				pageParam: {
					qid: id
				}
			});
		},
		//提出问题
		alertShow: function() {
			api.openWin({
				name: 'putQuestion',
				url: 'putQuestion.html',
				vScrollBarEnabled: false,
			});
		},
		del: function() {
			this.message = "";
		},
		//
		search: function() {
			if(this.message) {
				api.openWin({
					name: 'communitySearch',
					url: 'communitySearch.html',
					vScrollBarEnabled: false,
					pageParam: {
						key_name: this.message
					}
				});
			} else {
				api.toast({
					msg: '搜索内容不能为空',
					duration: 2000,
					location: 'middle'
				});
			}
		}
	}
})